package com.zhenyi.jiahua.task;

import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhStaff;
import com.jiahua.common.model.JhStudent;
import com.utils.DateUtils;
import com.zhenyi.jiahua.core.utils.MessageUtils;

import cn.hutool.core.text.StrFormatter;

public class JHContractTask implements Runnable {

	@Override
	public void run() {
		Date date = new DateTime().plusDays(30).toDate();
		String dt = DateUtils.dateFormat(date, DateUtils.YYYYMMDD);
		String dateString = dt + " 00:00:00";
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHContractDateStudents", dateString);
		List<JhStudent> students = JhStudent.dao.find(sqlPara);

		List<JhStaff> notiStaffs = JhStaff.dao.findByType(JhStaff.TYPE_PD);
		for (JhStudent jhStudent : students) {
			for (JhStaff jhStaff : notiStaffs) {
				String title = "合同到期提醒";
				String content = "学员{}合同将于{}到期";
				content = StrFormatter.format(content, jhStudent.getName(), dt);
				MessageUtils.send(jhStaff.getId(), null, title, content);
			}
		}
	}

}
